namespace IMPlant.Core.Data.Ado.BaseSupport
{
    using IMPlant.Core.Common.Attributes;
    using System;
    using System.Data;
    using System.Reflection;

    public class AutoOrm
    {
        public object MapRow<T>(IDataReader reader)
        {
            if (typeof(T) == typeof(int))
            {
                try
                {
                    return int.Parse(reader[0].ToString());
                }
                catch
                {
                    return default(T);
                }
            }
            if (typeof(T) == typeof(string))
            {
                return reader[0].ToString();
            }
            object obj2 = Activator.CreateInstance<T>();
            PropertyInfo[] properties = obj2.GetType().GetProperties();
            for (int i = 0; i < reader.FieldCount; i++)
            {
                foreach (PropertyInfo info in properties)
                {
                    object[] customAttributes = info.GetCustomAttributes(typeof(DBFileAttribute), false);
                    if ((customAttributes != null) && (customAttributes.Length > 0))
                    {
                        try
                        {
                            DBFileAttribute attribute = customAttributes[0] as DBFileAttribute;
                            if (attribute.DBFile.Trim().ToUpper() == reader.GetName(i).ToUpper())
                            {
                                info.SetValue(obj2, this.TypeToParse(reader[i], info.PropertyType), null);
                                break;
                            }
                            goto Label_0152;
                        }
                        catch (Exception exception)
                        {
                            throw exception;
                        }
                    }
                    if (info.Name.ToUpper() == reader.GetName(i).ToUpper())
                    {
                        info.SetValue(obj2, this.TypeToParse(reader[i], info.PropertyType), null);
                        break;
                    }
                Label_0152:;
                }
            }
            return obj2;
        }

        private object TypeToParse(object o, Type pT)
        {
            if (pT == typeof(int))
            {
                if (o == null)
                {
                    return 0;
                }
                if (o.ToString().ToUpper().Equals("TRUE"))
                {
                    return 1;
                }
                if (o.ToString().ToUpper().Equals("FALSE"))
                {
                    return 0;
                }
                try
                {
                    return int.Parse(o.ToString());
                }
                catch
                {
                    return 0;
                }
            }
            if (pT == typeof(string))
            {
                if (o == null)
                {
                    return "";
                }
                return o.ToString();
            }
            if (pT == typeof(DateTime))
            {
                try
                {
                    return DateTime.Parse(o.ToString());
                }
                catch
                {
                    return DateTime.MinValue;
                }
            }
            if (pT == typeof(Guid))
            {
                if (o == null)
                {
                    throw new Exception("DB GUID is NULL");
                }
                return new Guid(o.ToString());
            }
            if (pT != typeof(float))
            {
                return o;
            }
            if (o == null)
            {
                return float.Parse("0.0");
            }
            return float.Parse(o.ToString());
        }
    }
}

